//
//  UIScrollView+QuickDev.m
//  QuickDev
//
//  Created by 胡超 on 2020/7/20.
//  Copyright © 2020年 胡超., ltd. All rights reserved.

#import "UIScrollView+Mango.h"
#import "NSObject+Mango.h"

#import <objc/runtime.h>

//static void(*orig_UIScrollView_dealloc)(UIScrollView *, SEL);
//static void mm_UIScrollView_dealloc(UIScrollView *self, SEL _cmd) {
//    @try {
//        [self mg_removeObserverForKeyPath:@"contentOffset"];
//    }
//    @catch (NSException *exception) {}
//    @finally {}
//
//    orig_UIScrollView_dealloc(self, _cmd);
//}

@implementation UIScrollView (QuickDev)

+ (void)load {
    // unsafe
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        MGHookMessageEx(self.class, NSSelectorFromString(@"dealloc"), (IMP)mm_UIScrollView_dealloc, (IMP*)&orig_UIScrollView_dealloc);
//    });
}

- (UIView *)flexibleHeaderView {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setFlexibleHeaderView:(UIView *)flexibleHeaderView {
//    if (self.flexibleHeaderView || !flexibleHeaderView) {
//        return;
//    }
//    CGFloat headerHeight = flexibleHeaderView.bounds.size.height;
//    self.flexibleHeaderHeight = headerHeight;
//    self.contentInset = UIEdgeInsetsMake(headerHeight, self.contentInset.left, self.contentInset.bottom, self.contentInset.right);
//    flexibleHeaderView.frame = CGRectMake(0, -headerHeight, self.frame.size.width, headerHeight);
//    
//    [self insertSubview:flexibleHeaderView atIndex:0];
//    [self mg_setKVOSelector:@selector(qk_KVO_forKeyPath:ofObject:change:context:)];
//    [self mg_addObserverForKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew]; // 避免和其他category的KVO冲突

    objc_setAssociatedObject(self, @selector(qk_flexibleHeaderView), flexibleHeaderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)qk_setFlexibleHeaderImage:(UIImage *)image height:(CGFloat)height {
    if (self.flexibleHeaderView || !image || height <= 0) {
        return;
    }
    UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, height)];
    headerView.image = image;
    headerView.contentMode = UIViewContentModeScaleAspectFill;
    headerView.clipsToBounds = YES;
    self.flexibleHeaderView = headerView;
}

- (CGFloat)flexibleHeaderHeight {
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}

- (void)setFlexibleHeaderHeight:(CGFloat)height {
    objc_setAssociatedObject(self, @selector(qk_flexibleHeaderHeight), @(height), OBJC_ASSOCIATION_COPY_NONATOMIC);
}

#pragma mark - KVO

- (void)qk_KVO_forKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    CGFloat headerHeight = self.flexibleHeaderHeight;
    CGFloat offsetY = self.contentOffset.y + headerHeight;
    if (offsetY < 0) {
        self.flexibleHeaderView.frame = CGRectMake(0, offsetY-headerHeight, self.bounds.size.width, headerHeight - offsetY);
    } else {
        self.flexibleHeaderView.frame = CGRectMake(0, -headerHeight, self.bounds.size.width, headerHeight);
    }
}

@end
